<?php
/**
* openDocument library kit
*
* @link http://webappkit.net
* @package pathnodes
* @subpackage od
* @author J.Ducastel <nospam0@ducastel.name>
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
*/

/**
* content part of openDocuments (<office:document-content>)
*/
class odContentNode extends odXmlFragment {
/*--------------------------------------------------------------------
CONSTRUCTOR
--------------------------------------------------------------------*/
    /**
    * constructor alias
    * @access public
    * /
    function () {
        $this->__construct();
    }/**/
    
    /**
    * "real" constructor
    * @access public
    * /
    function __construct() {
        
    }/**/
/*--------------------------------------------------------------------
PUBLIC STATIC METHODS
--------------------------------------------------------------------*/
    /**
    * 
    * @static
    * @param
    * @return 
    * /
    function () {
        
    }/**/
/*--------------------------------------------------------------------
PUBLIC INSTANCE METHODS
--------------------------------------------------------------------*/
    /**
    * Executes some fusion treatments.
    * loops throught given associative array :
    * - searches for the corresponding <database-display> nodes and sets their value.
    * - keeps or remove <conditional-text> and <hidden-paragraph> elements
    * @param array $data associative array
    * @access public
    * @return int count of modified elements
    */
    function insertFusionRow($data=array(),$table=null,$db=null) {
        // echo $this->getDocumentXml();
        $count=0;
        // treating <database-display> elements, loops data array
        foreach ($data as $key => $value) {
            $xpath="//text:database-display[@text:column-name='$key']";
            //$xpath="//text:database-display";
            //$xpath="//urn:oasis:names:tc:opendocument:xmlns:text:1.0:database-display";
            $count+=$this->searchAndSetNodes($xpath,$value);
            //$nodes=$this->searchContent($xpath); echo "<p>$xpath=</p>"; print_r($nodes);
        }
        // treating <hidden-paragraph> elements
        // getting all paragraphs including a hidden-paragraph instruction element
        $xpath='//text:hidden-paragraph';
        $hiddenParagraphs=&$this->searchNodes($xpath); // echo wakaffvar($hiddenParagraphs);
        while($node=&current($hiddenParagraphs)) {
            if ($this->_processHiddenParagraph($node,$data))
                $count++;
            next($hiddenParagraphs);
        }
        // treating <conditional-text> elements
        $xpath='//text:conditional-text';
        $conditionalTexts=&$this->searchNodes($xpath); //print_r($nodes);
        while($node=&current($conditionalTexts)) {
            if ($this->_processConditionalText($node,$data))
                $count++;
            next($conditionalTexts);
        }
        //echo $this->getDocumentXml(); exit;
        // notify observers
        $this->_updated();
        return $count;
    }/**/
/*--------------------------------------------------------------------
VARS / MEMBERS
--------------------------------------------------------------------*/
    /**
    * @var type desc
    */
    //var //
    
/*--------------------------------------------------------------------
PRIVATE / PROTECTED METHODS
--------------------------------------------------------------------*/
    /**
    * get container instance
    * @param
    * @access protected
    * @return odContainer_zip|odContainer_xml|odContainer_folder
    * /
    function & _getContainer() {
        
    }/**/
    
    /**
    * processes hidden paragraph element upon data array
    * @var domElement $node hidden paragraph text:p node
    * @var array $data associative array data for conditions
    * @return bool
    */
    function _processHiddenParagraph(&$node,&$data) {
        // getting condition attribute
        $condition=$node->get_attribute('condition');
        if ($this->_evalConditionAttribute($condition,$data)) {
            // hide, remove parent node (paragraph)
            //echo "<p>$condition true, hiding paragraph</p>";
            // locating parent paragraph
            $fragment=&new odXmlFragment($node);
            return $fragment->searchAndRemoveNodes('//ancestor::text:p');
        } else {
            // show paragraph, remove hidden-paragraph node
            //echo "<p>$condition false, showing paragraph</p>";
            $parent=&$node->parent_node();
            if ($parent->remove_child($node))
                return true;
        }
        //echo "<p>$condition</p>";
        //trigger_error("echec processHiddenParagraph");
        return false;
        // echo "$condition=".wakaffvar();
    }/**/
    
    /**
     * processes conditional-text element
     */
    function _processConditionalText(&$node,&$data) {
        $condition=$node->get_attribute('condition');
        // getting appropriate text value
        if ($this->_evalConditionAttribute($condition,$data)) {
            $value=$node->get_attribute('string-value-if-true');
            //echo "<p>$condition true, => $value</p>";
        } else {
            $value=$node->get_attribute('string-value-if-false');
            //echo "<p>$condition false, => $value</p>";
        }
        // replacing node by text value
        $textNode=$this->doc->create_text_node($value);
        $node->replace_node($textNode);
        return true;
    }
    
    
    /**
     * evaluates condition attribute for hidden-paragraph elements
     * @var string $conditionAttribute
     * @var array $data
     * @return mixed
     */
    function _evalConditionAttribute($conditionAttribute,&$data) {
        // converting opendocument syntax to php
        $conditionAttribute=str_replace('ooow:','$',$conditionAttribute);
        extract($data);
        return eval('return '.$conditionAttribute.';');
    }/**/
}
?>